﻿using LD.Host;
using System;

namespace LD.Tool
{
    public partial class ToolPartNoChangeFrm : FrmToolBaseSkip// FrmToolBase
    {
        public ToolPartNoChangeFrm()
        {
            InitializeComponent();
            this.Shown += ToolMesSharpFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void ToolMesSharpFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            foreach (var item in Cache.Var)
            {
                this.cob_cache.Items.Add(item.Name);
            }
            foreach (var item in CommunicateFactory.Instance.TcpIp)
            {
                this.cob_tcpName.Items.Add(item.Key);
            }

            if (TB_Tool.Param != null)
            {
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ToolTcpIpParam>(TB_Tool.Param);
                if (param != null)
                {
                    this.cob_tcpName.Text = param.TcpIpName;
                    this.chk_thanResult.Checked = param.IsThanResult;
                    this.chk_saveResult.Checked = param.IsSaveResult;
                    this.txt_thanResult.Text = param.ThanResult;
                    this.cob_cache.Text = param.SaveResult;
                    this.num_timeOut.ValueNumber = param.TimeOut;
                    this.txt_cmd.Text = param.Cmd;
                    this.chk_splitChar.Checked = param.SplitChart;
                }
            }
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var param = new ToolTcpIpParam()
            {
                TcpIpName = this.cob_tcpName.Text,
                IsThanResult = this.chk_thanResult.Checked,
                ThanResult = this.txt_thanResult.Text,
                IsSaveResult = this.chk_saveResult.Checked,
                SaveResult = this.cob_cache.Text,
                TimeOut = (int)this.num_timeOut.ValueNumber,
                Cmd = this.txt_cmd.Text,
                SplitChart = this.chk_splitChar.Checked,
            };

            TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);
        }

        private void txt_thanResult_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
